//
//  ByViewShopCarToolBar.m
//  oc_knowledge
//
//  Created by pro on 17/1/13.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewShopCarToolBar.h"

@implementation ByViewShopCarToolBar
{
    UIControl   *_selectView;       // 全选
    UIButton    *_settleButton;     // 结算
    UILabel     *_totalPriceLabel;  // 合计
    UILabel     *_noteLabel;        // 备注
    
}
- (void)dealloc {
    
    [self removeObserver:self forKeyPath:@"selectAll"];
    [self removeObserver:self forKeyPath:@"totalPrice"];
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self setupUI];
        // 添加KVO
        [self addObserver:self forKeyPath:@"selectAll" options:NSKeyValueObservingOptionNew context:nil];
        [self addObserver:self forKeyPath:@"totalPrice" options:NSKeyValueObservingOptionNew context:nil];

    }
    return self;
}

#pragma mark - KVO事件通知方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    
    if ([keyPath isEqualToString:@"selectAll"]) {
        
        BOOL isSelectAll = [change[NSKeyValueChangeNewKey] boolValue];
        _selectView.selected = isSelectAll;
        UIButton *selectBtn = [_selectView viewWithTag:111];
        selectBtn.selected = isSelectAll;
    }
    if ([keyPath isEqualToString:@"totalPrice"]) {
        
        CGFloat totalPrice = [change[NSKeyValueChangeNewKey] floatValue];
        _totalPriceLabel.text = [NSString stringWithFormat:@"合计：￥%.2lf", totalPrice];

        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_totalPriceLabel.text];
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:[_totalPriceLabel.text rangeOfString:@"合计："]];
        [attrStr addAttribute:NSFontAttributeName value:FONT(12) range:[_totalPriceLabel.text rangeOfString:@"￥"]];
        [attrStr addAttribute:NSFontAttributeName value:FONT(12) range:NSMakeRange(_totalPriceLabel.text.length - 2, 2)];
        _totalPriceLabel.attributedText = attrStr;
    }
}

#pragma mark - 创建UI
- (void)setupUI {
    
    
    // 线条
    UIView *line = [UIView new];
    line.backgroundColor = BY_LINE_COLOR;
    [self addSubview:line];
 
    // 选中按钮
    _selectView = [UIControl new];
    [self addSubview:_selectView];
    [_selectView addTarget:self action:@selector(selectAll:) forControlEvents:UIControlEventTouchUpInside];
    //
    UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    selectBtn.userInteractionEnabled = false;
    [selectBtn setImage:[UIImage imageNamed:@"购物车未选中"] forState:UIControlStateNormal];
    [selectBtn setImage:[UIImage imageNamed:@"购物车选中"] forState:UIControlStateSelected];
    [_selectView addSubview:selectBtn];
    selectBtn.tag = 111;
    //
    UILabel *selectAllLabel = [[UILabel alloc] init];
    selectAllLabel.font = FONT(14);
    selectAllLabel.text = @"全选";
    selectAllLabel.textColor = [UIColor lightGrayColor];
    selectAllLabel.textAlignment = NSTextAlignmentCenter;
    [_selectView addSubview:selectAllLabel];
    
    // 结算按钮
    _settleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _settleButton.backgroundColor = BY_RED_COLOR;
    _settleButton.titleLabel.font = FONT(16);
    [_settleButton setTitle:@"结算" forState:UIControlStateNormal];
    [_settleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_settleButton addTarget:self action:@selector(settle) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_settleButton];
    
    // 合计label
    _totalPriceLabel = [[UILabel alloc] init];
    _totalPriceLabel.adjustsFontSizeToFitWidth = YES;
    _totalPriceLabel.font = FONT(16);
    _totalPriceLabel.text = @"合计：￥0.00";
    _totalPriceLabel.textColor = BY_RED_COLOR;
    _totalPriceLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_totalPriceLabel];
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_totalPriceLabel.text];
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:[_totalPriceLabel.text rangeOfString:@"合计："]];
    [attrStr addAttribute:NSFontAttributeName value:FONT(12) range:[_totalPriceLabel.text rangeOfString:@"￥"]];
    [attrStr addAttribute:NSFontAttributeName value:FONT(12) range:NSMakeRange(_totalPriceLabel.text.length - 2, 2)];
    _totalPriceLabel.attributedText = attrStr;
    
    // 备注
    _noteLabel = [[UILabel alloc] init];
    _noteLabel.adjustsFontSizeToFitWidth = YES;
    _noteLabel.font = FONT(14);
    _noteLabel.text = @"可积分抵扣";
    _noteLabel.textColor = [UIColor lightGrayColor];
    _noteLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_noteLabel];
    
    
    // 布局
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.height.equalTo(@1);
    }];
    [_selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(@0);
    }];
    [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(20, 20));
        make.left.equalTo(@5);
        make.centerY.equalTo(@0);
    }];
    [selectAllLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(@0);
        make.left.equalTo(selectBtn.mas_right).offset(10);
        make.right.equalTo(_selectView).offset(-5);
    }];
    [_settleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(@0);
        make.width.equalTo(@98);
    }];
    [_totalPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_settleButton.mas_left).offset(-10);
        make.bottom.equalTo(_settleButton.mas_centerY);
        make.height.equalTo(@20);
    }];
    [_noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_totalPriceLabel.mas_bottom);
        make.width.centerX.equalTo(_totalPriceLabel);
        make.height.equalTo(_totalPriceLabel);
    }];
    
}
#pragma mark - 点击事件
// 全选
- (void)selectAll:(UIControl *)sender {
    
    sender.selected = !sender.selected;
    self.selectAll = sender.selected;
    
    if (self.selectAllBlock) {
        self.selectAllBlock(sender.selected);
    }
}
// 结算
- (void)settle {
    
    if (self.settleBlock) {
        self.settleBlock();
    }
}


@end
